from django_filters.rest_framework import FilterSet
from django_filters.filters import *
from django_filters import FilterSet, CharFilter
from taggit.managers import TaggableManager
from core.filters import MultiTagFilter
from .models import Bug, BugOperate, BugComment


class BugFilterSet(FilterSet):
    name = CharFilter(lookup_expr="contains")
    number = CharFilter(lookup_expr="contains")
    start_time = DateFilter(
        field_name="create_time", lookup_expr="gte", label="开始日期"
    )
    end_time = DateFilter(field_name="create_time", lookup_expr="lt", label="结束日期")

    class Meta:
        model = Bug
        fields = [
            "number",
            "name",
            "product",
            "module",
            "version",
            "type",
            "priority",
            "gravity",
            "towner",
            "downer",
            "tags",
            "creator",
            "status",
            "start_time",
            "end_time",
        ]
        filter_overrides = {
            TaggableManager: {
                "filter_class": MultiTagFilter,
                "extra": lambda f: {
                    "lookup_expr": "icontains",
                    "field_name": "tags__name",
                },
            }
        }


class BugOperateFilterSet(FilterSet):

    class Meta:
        model = BugOperate
        fields = ["bug", "operation"]


class BugCommentFilterSet(FilterSet):

    class Meta:
        model = BugComment
        fields = [
            "bug",
        ]
